<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container {
            background: rgba(255, 255, 255, 0.9);
            width: 100%;
            max-width: 400px;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 15px 25px rgba(0, 0, 0, 0.2);
        }
        h2 {
            text-align: center;
            margin-bottom: 25px;
            color: #2c3e50;
            position: relative;
        }
        h2:after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 50px;
            height: 3px;
            background: #3498db;
        }
        .form-group {
            margin-bottom: 20px;
            position: relative;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #34495e;
            font-weight: 500;
        }
        input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: all 0.3s;
        }
        input:focus {
            border-color: #3498db;
            box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
            outline: none;
        }
        /* 邀请码特殊样式 */
        .invite-hint {
            font-size: 12px;
            color: #7f8c8d;
            margin-top: 4px;
        }
        .btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 12px;
            width: 100%;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 600;
            transition: background 0.3s;
        }
        .btn:hover {
            background: #2980b9;
        }
        .links {
            text-align: center;
            margin-top: 20px;
        }
        .links a {
            color: #3498db;
            text-decoration: none;
            font-weight: 500;
        }
        .links a:hover {
            text-decoration: underline;
        }
        /* 响应式优化 */
        @media (max-width: 480px) {
            .container {
                margin: 0 15px;
                padding: 20px;
            }
            input {
                padding: 10px 12px;
            }
        }
        /* 消息提示样式 */
        .message {
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 5px;
            text-align: center;
            font-weight: 500;
        }
        .message.success {
            background-color: #d4edda;
            color: #155724;
        }
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>创建新账户</h2>
    <div id="message" class="message" style="display: none;"></div>
    <form id="registerForm">
        <!-- 用户名 -->
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required> <!-- 字段名username匹配 -->
        </div>

        <!-- 密码 -->
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required> <!-- 字段名password匹配 -->
        </div>

        <!-- 邀请码 -->
        <div class="form-group">
            <label for="inviteCode">邀请码</label>
            <input type="text" id="inviteCode" name="inviteCode" required> <!-- 字段名inviteCode匹配 -->
        </div>

        <button type="submit" class="btn">注册</button>
    </form>
    <div class="links">
        <p>已有账户? <a href="/login">立即登录</a></p>
    </div>
</div>

<script>
    document.getElementById('registerForm').addEventListener('submit', function(e) {
        e.preventDefault();

        // 获取表单数据
        const formData = {
            username: document.getElementById('username').value,
            password: document.getElementById('password').value,
            inviteCode: document.getElementById('inviteCode').value
        };

        // 显示加载状态
        const messageBox = document.getElementById('message');
        messageBox.textContent = '正在注册...';
        messageBox.className = 'message';
        messageBox.style.display = 'block';

        // 发送AJAX请求到正确的API路径
        fetch('/api/auth/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: new URLSearchParams(formData)
        })
            .then(response => {
                // 处理非200状态码
                if (!response.ok) {
                    throw new Error(response.statusText);
                }
                return response.text();
            })
            .then(data => {
                // 处理响应
                messageBox.textContent = '注册成功，即将跳转到登录页面...';
                messageBox.className = 'message success';

                // 2秒后跳转到登录页面
                setTimeout(() => {
                    window.location.href = '/login';
                }, 2000);
            })
            .catch(error => {
                console.error('Error:', error);
                messageBox.textContent = '注册失败，请重试';
                messageBox.className = 'message error';
            });
    });
</script>
</body>
</html>